package com.dl7.mvp.video.ui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.dl7.mvp.base.baseadapter.MyFragmentPagerAdapter;
import com.dl7.mvp.api.RetrofitService;
import com.dl7.mvp.base.BaseFragment;
import com.dl7.mvp.video.bean.VideoCategoryList;
import com.dl7.mvp.mvp.R;
import com.dl7.mvp.mvp.databinding.FragmentVideoMainBinding;
import com.dl7.mvp.rxbus.RxBus;
import com.dl7.mvp.rxbus.RxBusBaseMessage;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import io.reactivex.disposables.Disposable;

public class VideoMainFragment extends BaseFragment<FragmentVideoMainBinding> {
    private ArrayList<String> mTitleList = new ArrayList<>();
    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private HashMap<Integer, List<VideoCategoryList.CategoryBean.AlbumBean>> mAlbumsListMap = new HashMap<>();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
        loadData();
    }

    @Override
    public int getContentViewResID() {
        return R.layout.fragment_video_main;
    }

    private void initFragmentList(List<VideoCategoryList.CategoryBean> data) {
        mTitleList.clear();
        mFragments.clear();
        //初始化导航标题,如果是title在json数据中,在初始化的时候可以使用异步任务加载的形式添加
        mTitleList.add("推荐");
        //初始化Fragment
        mFragments.add(new RcmdVideoFragment());
        if (data != null) {
            for (int i = 0; i < data.size(); i++) {
                VideoCategoryList.CategoryBean categoryBean = data.get(i);
                mTitleList.add(categoryBean.getName());
                mAlbumsListMap.put(categoryBean.getId(), categoryBean.getAlbums());
                mFragments.add(VideoAlbumListFragment.newInstance(categoryBean.getId()));
            }
        }
    }

    private void initView() {
        //getSupportFragmentManager()是Activity嵌套fragment时使用
        //getChildFragmentManager()是Fragment嵌套Fragment时使用
        FragmentPagerAdapter adapter = new MyFragmentPagerAdapter(getChildFragmentManager(), mFragments, mTitleList);
        bindingView.viewPager.setAdapter(adapter);
        // 左右预加载页面的个数
        bindingView.viewPager.setOffscreenPageLimit(3);
        adapter.notifyDataSetChanged();
        bindingView.tableLayout.setTabMode(TabLayout.MODE_FIXED);
        //设置Viewpager和Tablayout进行联动
        bindingView.tableLayout.setupWithViewPager(bindingView.viewPager);
        // item点击跳转
        // initRxBus();
    }

    void loadData() {
        RetrofitService.getVideoCategoryList(0, 100, 20)
                .subscribe(categoryBeans -> {
                    initFragmentList(categoryBeans);
                    bindingView.viewPager.getAdapter().notifyDataSetChanged();
                });
    }

    private void initRxBus() {
        Disposable disposable = RxBus.getInstance().register(RxBusBaseMessage.class)
                .subscribe(msg -> {
                    switch (msg.getCode()) {
                        case 0:
                            bindingView.viewPager.setCurrentItem(3);
                            break;
                        case 1:
                            bindingView.viewPager.setCurrentItem(1);
                            break;
                        case 2:
                            bindingView.viewPager.setCurrentItem(2);
                            break;
                    }
                });
        addDisposable(disposable);
    }

    public List<VideoCategoryList.CategoryBean.AlbumBean> getAlbumsList(int id) {
        return mAlbumsListMap.get(id);
    }
}
